package sk.fiit.lulco.grafy;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileParser {

	/**
	 * method for create matrix of graph
	 * 
	 * @param String
	 *            filename
	 * @return int[][]
	 */
	public ParsedData parse(String filename) throws IOException {
		int[][] matrix = null;
		int[] initialColors = null;
		int chromaticNumber = 0;
		int bestColoring = 0;

		FileReader reader = new FileReader(new File(filename));
		BufferedReader buf = new BufferedReader(reader);

		String line = null;
		while ((line = buf.readLine()) != null) {
			String[] lineParts = line.split(" ");
			if (lineParts[0].equals("p") && lineParts[1].equals("edge")) {
				int size = Integer.parseInt(lineParts[2]) + 1;
				matrix = new int[size][size];
				initialColors = new int[size];
			} else if (lineParts[0].equals("e")) {
				int x = Integer.parseInt(lineParts[1]);
				int y = Integer.parseInt(lineParts[2]);
				matrix[x][y] = 1;
				matrix[y][x] = 1;
			} else if (lineParts[0].equals("i")) {
				int x = Integer.parseInt(lineParts[1]);
				int y = Integer.parseInt(lineParts[2]);
				initialColors[x] = y;
			} else if (lineParts[0].equals("ch")) {
				chromaticNumber = Integer.parseInt(lineParts[1]);
			} else if (lineParts[0].equals("b")) {
				int newValue = Integer.parseInt(lineParts[1]);
				if (bestColoring == 0) {
					bestColoring = newValue;
				} else {
					bestColoring = bestColoring > newValue ? newValue : bestColoring;
				}
			}
		}

		ParsedData output = new ParsedData();
		output.setMatrix(matrix);
		output.setInitialColors(initialColors);
		output.setChromaticNumber(chromaticNumber);
		output.setBestColoring(bestColoring);

		return output;
	}
}

class ParsedData {

	private int[][] matrix;

	private int[] initialColors;

	private int chromaticNumber;

	private int bestColoring;

	public void setMatrix(int[][] matrix) {
		this.matrix = matrix;
	}

	public int[][] getMatrix() {
		return this.matrix;
	}

	public void setInitialColors(int[] initialColors) {
		this.initialColors = initialColors;
	}

	public int[] getInitialColors() {
		return this.initialColors;
	}

	public void setChromaticNumber(int chromaticNumber) {
		this.chromaticNumber = chromaticNumber;
	}

	public int getChromaticNumber() {
		return this.chromaticNumber;
	}

	public void setBestColoring(int bestColoring) {
		this.bestColoring = bestColoring;
	}

	public int getBestColoring() {
		return this.bestColoring;
	}
}